package me.hetong.caigou.ui;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import me.hetong.caigou.entity.Qipu;
import me.hetong.caigou.util.FxUtils;

public class EditPuPage {
    private static Stage stage;
    private static VBox vBox;
    private static Scene scene;
    private static TextField title;
    private static TextField matchName;
    private static TextField matchDate;
    private static TextField red;
    private static TextField redTeam;
    private static TextField black;
    private static TextField blackTeam;
    private static TextField result;
    private static TextField timeRule;
    private static TextField lun;

    private EditPuPage() {
    }

    public static void init(Qipu qipu) {

        vBox = FxUtils.createVBox();
        vBox.setPadding(new Insets(10));
        vBox.setSpacing(5);
        scene = new Scene(vBox);
        stage = new Stage();
        stage.setScene(scene);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setTitle("修改棋谱基本信息");

        title = FxUtils.createTextField(qipu.getTitle());
        matchName = FxUtils.createTextField(qipu.getMatch());
        matchDate = FxUtils.createTextField(qipu.getMatchDate());
        red = FxUtils.createTextField(qipu.getRed());
        redTeam = FxUtils.createTextField(qipu.getRedTeam());
        black = FxUtils.createTextField(qipu.getBlack());
        blackTeam = FxUtils.createTextField(qipu.getBlackTeam());
        result = FxUtils.createTextField(qipu.getResult());
        timeRule = FxUtils.createTextField(qipu.getTimeRule());
        lun = FxUtils.createTextField(qipu.getLun());

        vBox.getChildren().add(row("比赛日期", matchDate));
        vBox.getChildren().add(row("比赛名称", matchName));
        vBox.getChildren().add(row("显示标题", title));
        vBox.getChildren().add(row("比赛轮局", lun));
        vBox.getChildren().add(row("计时规则", timeRule));
        vBox.getChildren().add(row("红方棋手", red));
        vBox.getChildren().add(row("红方团队", redTeam));
        vBox.getChildren().add(row("黑方棋手", black));
        vBox.getChildren().add(row("黑方团队", blackTeam));
        vBox.getChildren().add(row("比赛结果", result));

        Button closeBtn = FxUtils.createButton("关闭");
        closeBtn.setOnAction(event -> {
            stage.close();
        });

        Button saveBtn = FxUtils.createButton("保存");
        saveBtn.setOnAction(event -> {
            qipu.setTitle(title.getText());
            qipu.setMatch(matchName.getText());
            qipu.setMatchDate(matchDate.getText());
            qipu.setRed(red.getText());
            qipu.setRedTeam(redTeam.getText());
            qipu.setBlack(black.getText());
            qipu.setBlackTeam(blackTeam.getText());
            qipu.setResult(result.getText());
            qipu.setTimeRule(timeRule.getText());
            qipu.setLun(lun.getText());

//            Global.saveWen();
//            Global.queryWen(wen.getId());
//            Global.openWen(wen);
            stage.close();
        });

        HBox hBox = FxUtils.createHBox();
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER_RIGHT);
        hBox.getChildren().add(saveBtn);
        hBox.getChildren().add(closeBtn);

        vBox.getChildren().add(hBox);

        stage.show();
    }

    public static HBox row(String text, TextField textField) {

        Label label = FxUtils.createLabel(text);
        label.setPadding(new Insets(0, 10, 0, 0));

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().add(label);
        hBox.getChildren().add(textField);

        textField.setPrefWidth(300);

        return hBox;
    }
}
